using System.Collections.Generic;
using DG.Tweening;
using Gameplay.PVE.Entity;
using UnityEngine;
using Yoozoo.Managers;

namespace Gameplay.PVE
{
    public class SpecialCaseManager
    {
        public static SpecialCaseManager Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new SpecialCaseManager();
                }
                return instance;
            }
        }
        
        private static SpecialCaseManager instance;

        private List<UnitBase> playerList = new List<UnitBase>();
        private List<UnitBase> subPlayerList = new List<UnitBase>();

        private int playType;

        private int stageId;

        private List<StageBase> stageList = new List<StageBase>();

        private StageBase currentStage;
        
        
        public void Initialize()
        {
           
        }

        public void Dispose()
        {
            playerList.Clear();
            subPlayerList.Clear();
            stageList.Clear();
            currentStage = null;
            instance = null;
        }

        public void NewPlay(int playType)
        {
            playerList.Clear();
            subPlayerList.Clear();
            this.playType = playType;
        }

        
        public void AddPlayer(UnitBase player)
        {
            player.Data.isInSpecialCase = true;
            player.Data.SetTargetPosition(player.Data.position);
            AddOrderTransmitArg arg = ClassManager.Get<AddOrderTransmitArg>();
            var order = ClassManager.Get<UnitOrder>();
            order.type = UnitOrder.EOrderType.SpecialCase;
            arg.order = order;
            player.Transmit(ETransmitType.AddOrder,arg);
            playerList.Add(player);
        }


        public void AddSubPlayer(UnitBase player)
        {
            player.Data.isInSpecialCase = true;
            subPlayerList.Add(player);
        }

        public void Start()
        {
            stageList.Clear();
            stageId = 0;
            var moveStage = ClassManager.Get<MoveStage>();
            stageList.Add(moveStage);
            moveStage.AddAction(() =>
            {
                /*PveCameraManager.Instance.FocusSpecial(
                    PveManager.Instance.GetStagePosition() + new Vector3(69.45f - 100,2.72f,103.8f - 100),
                    new Vector3(11.2f,119.2f,0),
                    Ease.Linear,1.5f);*/
                //var forward = ((subPlayerList[0].Data.position - playerList[0].Data.position).normalized + Vector3.right).normalized;
                //PveCameraManager.Instance.RotateSpecial();
            },0);
            moveStage.SetDuration(0.8f);
            moveStage = ClassManager.Get<MoveStage>();
            moveStage.AddAction(() =>
            {
                /*PveCameraManager.Instance.FocusSpecial(
                    PveManager.Instance.GetStagePosition() + new Vector3(84 - 100,2f,96.7f - 100),
                    new Vector3(8.78f,-180,0),
                    Ease.InOutQuad,1.6f);*/
            },1.5f);
            moveStage.SetDuration(3.1f);
            stageList.Add(moveStage);
            var animatorStage = ClassManager.Get<AnimatorStage>();
            animatorStage.AddAction(() =>
            {
                for (int i = 0; i < Random.Range(12,20); i++)
                {
                    
                }
            },4);
            /*animatorStage.AddAction(() =>
            {
                for (int i = 0; i < Random.Range(6,12); i++)
                {
                    PveManager.Instance.SpawnItem(subPlayerList[0].Data.position + new Vector3(0,0,1f),PveDropType.Money,1);
                }
            },5);
            animatorStage.AddAction(() =>
            {
                for (int i = 0; i < Random.Range(6,12); i++)
                {
                    PveManager.Instance.SpawnItem(subPlayerList[0].Data.position + new Vector3(0,0,1f),PveDropType.Money,1);
                }
            },6);*/
            stageList.Add(animatorStage);
            moveStage = ClassManager.Get<MoveStage>();
            moveStage.SetDuration(1);
            moveStage.AddAction(() =>
            {
                //PveCameraManager.Instance.EndFocusSpecial();
            },0);
            stageList.Add(moveStage);
            StartStage(0);
        }

        private void StartStage(int stageId)
        {
            if (stageId < stageList.Count)
            {
                currentStage = stageList[stageId];
                currentStage.Start(stageId,playerList,subPlayerList);
            }
            else
            {
                currentStage = null;
                for (int i = 0; i < playerList.Count; i++)
                {
                    playerList[i].Data.isInSpecialCase = false;
                    var orders = playerList[i].Data.GetOrders();
                    orders.RemoveAt(0);
                }
                
                //PveCameraManager.Instance.EndFocusSpecial();
            }
            
        }

        public void Update()
        {
            CheckPlayerState();
        }

        private void CheckPlayerState()
        {
            if (currentStage != null)
            {
                currentStage.Update();
                if (currentStage.IsFinish())
                {
                    Debug.Log("全员就绪");
                    stageId++;
                    StartStage(stageId);
                }
            }
        }
    }
}